<?php
declare (strict_types = 1);

namespace app\API\controller;

use app\API\model\course;
use app\API\model\project;
use app\API\server\RedisInstance;
use Elasticsearch\ClientBuilder;
use think\cache\driver\Redis;
use think\Request;

class search
{
//    public function index()
//    {
//        $client=ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
//        $parms=[
//            'index'=>'show', //索引名
//            'body'=>[
//                'settings'=>[
//                    'number_of_shards'=>5, //分片数量（后期不可更改）
//                    'number_of_replicas'=>1 //副本数量（后期可更改）
//                ],
//
//                'mappings'=>[
////                          'table'=>[
////                              'enabled'=>true //php版本7.0之后表名默认为‘_doc’,表名可以不加
////                        ],
//                    //加载ik分词器的字段
//                    'properties' => [
//                        'fang_name' => [
//                            'type' => 'text',
//                            'analyzer' => 'ik_max_word',
//                            'search_analyzer' => 'ik_max_word'
//                        ],
//                    ]
//                ],
//            ]
//        ];
//        $response=$client->indices()->create($parms);
//        print_r($response);
//    }

    //要求搜索的课程名称
    public function reads(Request $request){
      $name =  $request['name'];
      if (empty($name)){
          return json(['code'=>0,'msg'=>'失败','data'=>null]);
      }
        $client=ClientBuilder::create()->build();
        $params=[
            'index'=>'show',
            'type'=>'_doc',
            'body'=>[
                'query'=>[
                    'match'=>['course_title'=>$name]
                ]
            ]
        ];
        $results = $client->search($params);
        $his =$results['hits']['hits'];
        $name =[];
        foreach ($his as $item=>$value){
            $name[$item] = $value['_source'];
        }
        return json(['code'=>0,'msg'=>'成功','data'=>$name]);
    }

    //首页限时优惠模块查询内容
    //最热
    public function selHot(Request $request){
        $page=$request->get('page');
        $selPage = "selHot_$page";
        $selPageName = RedisInstance::getString($selPage);
        if (!empty($selPageName)){
            return json(['code'=>200,'data'=>json_decode($selPageName,true),'msg'=>'ok']);
        }
        $result=course::order('number_of','desc')->paginate(5);
        $result&&$result=$result->toArray();

        RedisInstance::setString($selPage,json_encode($result));
        return json(['code'=>200,'data'=>$result,'msg'=>'ok']);
    }
    //最新
    public function selNewest(Request $request){
        $page=$request->get('page');
            $selNewest = "selNewest_$page";
        $selNewestName = RedisInstance::getString($selNewest);
        if (!empty($selNewestName)){
            return json(['code'=>200,'data'=>json_decode($selNewestName,true),'msg'=>'ok']);
        }
        $result=course::order('created_time','desc')->paginate(5);
        $result&&$result=$result->toArray();
            RedisInstance::setString($selNewest,json_encode($result));
        return json(['code'=>200,'data'=>$result,'msg'=>'ok']);
    }
        //数据添加
//    public function add()
//    {
////        $data=course::where('course_title','like',$name)->select();
//        $data = course::select();
//        $data&&$data=$data->toArray();
//        foreach ($data as $k=>$v)
//        {
//            $da=$v;
//            $client=ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
//            $parms=[
//                'index'=>'show',
//                'type'=>'_doc',
//                'body'=>[
//                    'id'=>$da['id'],
//                    'course_title'=>$da['course_title'],
//                    'course_img'=>$da['course_img'],
//                    'number_of'=>$da['number_of'],
//                    'price'=>$da['price'],
//                    'price_original'=>$da['price_original'],
//                    'status'=>$da['status'],
//                    'created_time'=>$da['created_time'],
//                    'updated_time'=>$da['updated_time'],
//                ]
//            ];
//            $response=$client->index($parms);
//        }
//        if (empty($response)){
//            return json(['code'=>1,'msg'=>'失败','data'=>$response]);
//        }
//        return json(['code'=>0,'msg'=>'成功','data'=>$response]);
//    }
}
